package com.example.litterpainterdemo.db

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.litterpainterdemo.utils.ContextHelper

//数据库
@Database(
    entities = [Picture::class],
    version = 1,
    exportSchema = false
)
@TypeConverters(BitmapConverter::class)
abstract class PictureDatabase:RoomDatabase() {
    abstract fun getDao():PictureDao

    //全局只有一个数据库对象
    companion object{
        private var instance:PictureDatabase? = null

        fun db():PictureDatabase{
            if(instance != null) return instance!!

            synchronized(this) {
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        ContextHelper.getContext.get()!!,
                        PictureDatabase::class.java,
                        "picture.db"
                    ).build()
                }
            }

            return instance!!
        }
    }
}